package settings_api

import (
	"ginBolg/config"
	"ginBolg/core"
	"ginBolg/global"
	"ginBolg/models/res"
	"github.com/gin-gonic/gin"
)

type SettingsUri struct {
	Name string `uri:"name" binding:"required"`
}

func (SettingsApi) SettingsInfoView(c *gin.Context) {
	var cr SettingsUri
	err := c.ShouldBindUri(&cr)
	if err != nil {
		res.FailWithCode(res.ArgumentError, c)
		return
	}
	switch cr.Name {
	case "site":
		res.OkWithData(global.Config.SiteInfo, c)
	case "mysql":
		res.OkWithData(global.Config.Mysql, c)
	case "system":
		res.OkWithData(global.Config.SiteInfo, c)
	default:
		res.Fail("配置名称未找到", c)
	}
}

func (SettingsApi) SettingsInfoUpdateView(c *gin.Context) {
	var cr config.SiteInfo
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithCode(res.ArgumentError, c)
		return
	}
	global.Config.SiteInfo = cr
	err = core.SetYaml()
	if err != nil {
		global.Log.Error(err)
		res.Fail(err.Error(), c)
		return
	}
	res.Ok(c)
}
